package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.*;
import com.zb.mapper.ZjWorkLeaveMessageMapper;
import com.zb.result.PageUtil;
import com.zb.service.*;
import com.zb.vo.OrderLeaveMessage;
import org.jacoco.agent.rt.internal_1f1cc91.core.internal.flow.IFrame;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 工单留言表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-10-24
 */
@Service
public class ZjWorkLeaveMessageServiceImpl extends ServiceImpl<ZjWorkLeaveMessageMapper, ZjWorkLeaveMessage> implements ZjWorkLeaveMessageService {
    @Resource
    private ZjWorkLeaveMessageMapper workLeaveMessageMapper;
    @Resource
    private ZjWorkLeaveMessage02Service workLeaveMessage02Service;
    @Resource
    private ZjWorkLeaveMessage03Service workLeaveMessage03Service;
    @Resource
    private ZjWorkLeaveMessage04Service workLeaveMessage04Service;
    @Resource
    private ZjWorkLeaveMessage05Service workLeaveMessage05Service;
    @Resource
    private ZjWorkLeaveMessageAnnexService workLeaveMessageAnnexService;
    /**
     * 分表数量
     */
    private final int  TABLE_COUNT = 5;

    @Override
    public void save(List<OrderLeaveMessage> messageList) {
        if (CollectionUtil.isEmpty(messageList)) {
            return;
        }
        List<ZjWorkLeaveMessage> messages = new ArrayList<>();
        List<ZjWorkLeaveMessageAnnex> messageAnnexes = new ArrayList<>();
        ZjWorkLeaveMessage message = null;
        ZjWorkLeaveMessageAnnex messageAnnex = null;
        for (OrderLeaveMessage me : messageList) {
            message = new ZjWorkLeaveMessage();
            BeanUtil.copyProperties(me, message);
            message.setId(IdWorker.getId());
            message.setVisitType(me.getVisitType());
            message.setEmpType(me.getEmpType());
            message.setEmpRead(1 == me.getEmpType());
            message.setNetworkRead(2 == me.getEmpType() || 0 != me.getVisitType());
            message.setEngineerRead(3 == me.getEmpType() || 0 != me.getVisitType());
            message.setUserRead(4 == me.getEmpType() || 0 != me.getVisitType());
            messages.add(message);
            if (CollectionUtil.isNotEmpty(me.getAnnexs())) {
                for (String annex : me.getAnnexs()) {
                    messageAnnex = new ZjWorkLeaveMessageAnnex();
                    messageAnnex.setId(IdWorker.getId());
                    messageAnnex.setCustAccId(me.getCustAccId());
                    messageAnnex.setWorkId(me.getWorkId());
                    messageAnnex.setWorkLeaveMessageId(message.getId());
                    messageAnnex.setPath(annex);
                    messageAnnexes.add(messageAnnex);
                }
            }
        }
        save(messages, messageAnnexes);
    }

    /**
     * 留言数据封装
     * @param visitType 可见类型：0-公开，1-服务商，2-工程师，3-工单客户,4-工单客服
     * @param empType 操作人员类型：1-客服，2-服务商，3-工程师，4-工单客户
     * @param messageList
     * @return
     */
    public void getMessageData(final Integer visitType, final Integer empType, List<ZjWorkLeaveMessage> leaveMessageList, List<ZjWorkLeaveMessageAnnex> annexList, List<OrderLeaveMessage> messageList) {
        ZjWorkLeaveMessage message = null;
        ZjWorkLeaveMessageAnnex messageAnnex = null;
        for (OrderLeaveMessage me : messageList) {
            message = new ZjWorkLeaveMessage();
            BeanUtil.copyProperties(me, message);
            message.setId(IdWorker.getId());
            message.setVisitType(visitType);
            message.setEmpType(empType);
            message.setEmpRead(1 == empType);
            message.setNetworkRead(2 == empType || 0 != visitType);
            message.setEngineerRead(3 == empType || 0 != visitType);
            message.setUserRead(4 == empType || 0 != visitType);
            leaveMessageList.add(message);

            if (CollectionUtil.isNotEmpty(me.getAnnexs())) {
                for (String annex : me.getAnnexs()) {
                    messageAnnex = new ZjWorkLeaveMessageAnnex();
                    messageAnnex.setId(IdWorker.getId());
                    messageAnnex.setCustAccId(me.getCustAccId());
                    messageAnnex.setWorkId(me.getWorkId());
                    messageAnnex.setWorkLeaveMessageId(message.getId());
                    messageAnnex.setPath(annex);
                    annexList.add(messageAnnex);
                }
            }
        }
    }

    public void save(List<ZjWorkLeaveMessage> leaveMessageList, List<ZjWorkLeaveMessageAnnex> annexList) {
        List<ZjWorkLeaveMessage> messages = new ArrayList<>();
        List<ZjWorkLeaveMessage02> message02s = new ArrayList<>();
        List<ZjWorkLeaveMessage03> message03s = new ArrayList<>();
        List<ZjWorkLeaveMessage04> message04s = new ArrayList<>();
        List<ZjWorkLeaveMessage05> message05s = new ArrayList<>();
        long index = 0;
        for (ZjWorkLeaveMessage message : leaveMessageList) {
            index = message.getId() % TABLE_COUNT;
            if (index == 0) {
                messages.add(message);
                continue;
            }
            if (index == 1) {
                message02s.add(new ZjWorkLeaveMessage02(message));
                continue;
            }
            if (index == 2) {
                message03s.add(new ZjWorkLeaveMessage03(message));
                continue;
            }
            if (index == 3) {
                message04s.add(new ZjWorkLeaveMessage04(message));
                continue;
            }
            if (index == 4) {
                message05s.add(new ZjWorkLeaveMessage05(message));
            }
        }
        if (!messages.isEmpty()) this.saveBatch(messages);
        if (!message02s.isEmpty()) workLeaveMessage02Service.saveBatch(message02s);
        if (!message03s.isEmpty()) workLeaveMessage03Service.saveBatch(message03s);
        if (!message04s.isEmpty()) workLeaveMessage04Service.saveBatch(message04s);
        if (!message05s.isEmpty()) workLeaveMessage05Service.saveBatch(message05s);
        workLeaveMessageAnnexService.save(annexList);
    }


    // 根据工单ID获取客服未读的工单留言
    @Override
    public long getWorkNotReadLeaveMessageCount(Long workId) {
        return workLeaveMessageMapper.getWorkNotReadLeaveMessageCount(workId);
    }

    // 根据工单ID获取工单留言
    @Override
    public List<ZjWorkLeaveMessage> getWorkLeaveMessageList(Long workId, PageUtil pageUtil) {
        return workLeaveMessageMapper.getWorkLeaveMessageList(workId, pageUtil);
    }

    // 根据工单ID获取客服工单留言总记录数
    @Override
    public long getWorkLeaveMessageListCount(Long workId) {
        return workLeaveMessageMapper.getWorkLeaveMessageListCount(workId);
    }

    // 根据工单ID获取服务商负责人的工单留言
    @Override
    public List<ZjWorkLeaveMessage> getNetworkWorkLeaveMessageList(Long empId, Long workId, PageUtil pageUtil) {
         return workLeaveMessageMapper.getNetworkWorkLeaveMessageList(empId, workId, pageUtil);
    }

    // 根据工单ID获取服务商负责人的工单留言总记录数
    @Override
    public long getNetworkWorkLeaveMessageListCount(Long empId, Long workId) {
        return workLeaveMessageMapper.getNetworkWorkLeaveMessageListCount(empId, workId);
    }

    // 根据工单ID获取服务商工程师的工单留言
    @Override
    public List<ZjWorkLeaveMessage> getEngineerWorkLeaveMessageList(Long empId, Long workId, PageUtil pageUtil) {
        return workLeaveMessageMapper.getEngineerWorkLeaveMessageList(empId, workId, pageUtil);
    }

    // 根据工单ID获取服务商工程师的工单留言总记录数
    @Override
    public long getEngineerWorkLeaveMessageListCount(Long empId, Long workId) {
        return workLeaveMessageMapper.getEngineerWorkLeaveMessageListCount(empId, workId);
    }

    // 根据工单ID更新工单客服未读留言
    @Override
    @Transactional
    public void updateWorkEmpNotReadLeaveMessageByWorkId(Long workId) {
        workLeaveMessageMapper.updateWorkEmpNotReadLeaveMessageByWorkId(workId);
        workLeaveMessage02Service.updateWorkEmpNotReadLeaveMessageByWorkId(workId);
        workLeaveMessage03Service.updateWorkEmpNotReadLeaveMessageByWorkId(workId);
        workLeaveMessage04Service.updateWorkEmpNotReadLeaveMessageByWorkId(workId);
        workLeaveMessage05Service.updateWorkEmpNotReadLeaveMessageByWorkId(workId);
    }

    // 根据工单ID更新服务商未读留言
    @Override
    public void updateNetworkNotReadLeaveMessageByWorkId(Long workId, boolean engineer) {
        workLeaveMessageMapper.updateNetworkNotReadLeaveMessageByWorkId(workId, engineer);
        workLeaveMessage02Service.updateNetworkNotReadLeaveMessageByWorkId(workId, engineer);
        workLeaveMessage03Service.updateNetworkNotReadLeaveMessageByWorkId(workId, engineer);
        workLeaveMessage04Service.updateNetworkNotReadLeaveMessageByWorkId(workId, engineer);
        workLeaveMessage05Service.updateNetworkNotReadLeaveMessageByWorkId(workId, engineer);
    }

    // 根据工单ID更新工程师未读留言
    @Override
    public void updateEngineerNotReadLeaveMessageByWorkId(Long workId) {
        workLeaveMessageMapper.updateEngineerNotReadLeaveMessageByWorkId(workId);
        workLeaveMessage02Service.updateEngineerNotReadLeaveMessageByWorkId(workId);
        workLeaveMessage03Service.updateEngineerNotReadLeaveMessageByWorkId(workId);
        workLeaveMessage04Service.updateEngineerNotReadLeaveMessageByWorkId(workId);
        workLeaveMessage05Service.updateEngineerNotReadLeaveMessageByWorkId(workId);
    }

    // 根据工单ID更新工单客户未读留言
    @Override
    public void updateUserNotReadLeaveMessageByWorkId(Long workId) {
        workLeaveMessageMapper.updateUserNotReadLeaveMessageByWorkId(workId);
        workLeaveMessage02Service.updateUserNotReadLeaveMessageByWorkId(workId);
        workLeaveMessage03Service.updateUserNotReadLeaveMessageByWorkId(workId);
        workLeaveMessage04Service.updateUserNotReadLeaveMessageByWorkId(workId);
        workLeaveMessage05Service.updateUserNotReadLeaveMessageByWorkId(workId);
    }

    // 根据工单ID获取服务商负责人或工程师最新的一条留言
    @Override
    public List<ZjWorkLeaveMessage> getNetworkLastOneMesageByWorkIds(boolean manager, List<Long> workIds) {
        return workLeaveMessageMapper.getNetworkLastOneMesageByWorkIds(manager, workIds);
    }

    // 根据工单ID获取服务商负责人或工程师未读的留言
    @Override
    public List<QtyVo> getNetworkNotReadQtyList(boolean manager, List<Long> workIds) {
        return workLeaveMessageMapper.getNetworkNotReadQtyList(manager, workIds);
    }

    // 获取服务商未读留言数量
    @Override
    public long getNetworkUnreadMessageQty(Long custAccId, Long networkId) {
        return workLeaveMessageMapper.getNetworkUnreadMessageQty(custAccId, networkId);
    }

    // 获取工程师未读留言数量
    @Override
    public long getNetworkEmpUnReadMessageQty(Long custAccId, Long networkId, Long networkEmpId) {
        return workLeaveMessageMapper.getNetworkEmpUnReadMessageQty(custAccId, networkId, networkEmpId);
    }

    // 根据工单ID获取工单最新留言
    @Override
    public List<ZjWorkLeaveMessage> getLastByWorkIds(List<Long> workIds) {
        return workLeaveMessageMapper.getLastByWorkIds(workIds);
    }

    // 根据工单ID获取留言信息
    @Override
    public List<ZjWorkLeaveMessage> getByWorkIds(List<Long> workIds) {
        if (CollectionUtil.isEmpty(workIds)) {
            return new ArrayList<>();
        }
        return workLeaveMessageMapper.getByWorkIds(workIds);
    }
}
